package com.itheima;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class MapDemo {
    public static void main(String[] args) {
        test0();
        test1();
        test2();
        test3();
    }

    public static void test3() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 98.5);
        map.put("李四", 89.5);
        map.put("王五", 99.5);
        map.put("赵六", 79.5);
        map.forEach(new BiConsumer<String, Double>() {
            @Override
            public void accept(String key, Double value) {
                System.out.println(key + ":" + value);
            }
        });
        map.forEach((key,value)-> System.out.println(key + ":" + value));
    }

    public static void test2() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 98.5);
        map.put("李四", 89.5);
        map.put("王五", 99.5);
        map.put("赵六", 79.5);
        //1.将Map集合转换成set集合（将map中的key-value绑定成一个对象entry）
        Set<Map.Entry<String, Double>> entrySet = map.entrySet();
        //2.遍历set集合
        for (Map.Entry<String, Double> entry : entrySet) {
            //3.根据entry获取key和value
            String key = entry.getKey();
            Double value = entry.getValue();
            System.out.println(key + ":" + value);
        }

    }
    public static void test1() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 98.5);
        map.put("李四", 89.5);
        map.put("王五", 99.5);
        map.put("赵六", 79.5);
        //将Map中的键值取出来放到set集合中
        Set<String> keySet = map.keySet();
        //遍历key集合
        for (String key : keySet) {
            //根据key去找值
            Double value = map.get(key);
            System.out.println(key + ":" + value);
        }
    }
    private static void test0() {
        Map<String, Double> map = new HashMap<>();
        //1.增，添加元素
        map.put("张三", 98.5);
        map.put("李四", 89.5);
        map.put("王五", 99.5);
        map.put("赵六", 79.5);
        map.put("赵六", 88.5);
        System.out.println(map);

        //2.删，根据键删除键值对应的元素
        map.remove("张三");
        System.out.println(map);

        //3.判断是否包含指定的键
        boolean b = map.containsKey("张三");
        System.out.println("b = "+ b);

        //4.判断是否包含指定的值
        boolean b1 = map.containsValue(98.5);
        System.out.println("b1 = "+ b1);

        //5.判断是否为空
        boolean empty = map.isEmpty();
        System.out.println("empty = "+ empty);

        //6.获取集合长度，及键和值的个数
        int size = map.size();
        System.out.println("size = "+ size);

        //7.清空集合
        map.clear();
        System.out.println(map);
    }
}
